package com.lancoo.answer.ui.answerSheet.vh;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.entity.AnswerSheetBean;
import com.lancoo.answer.model.entity.AnswerSheetChildBean;
import com.lancoo.answer.model.eventBean.AnswerSheetEventBean;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.widget.ChildFlowLayout;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by pengmi on 2022/2/22 9:06
 * Description:等级考试场景VH
 */
public class GradeTestVH extends AnswerSheetBaseVH{

    private int enableShowAnswerScore;

    public static GradeTestVH getGradeTestVH(@NonNull ViewGroup parent,int enableShowAnswerScore){
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.ev_item_type_grade_test_answer_sheet, parent, false);
        return new GradeTestVH(inflate,enableShowAnswerScore);
    }


    public GradeTestVH(@NonNull View itemView, int enableShowAnswerScore) {
        super(itemView);
        this.enableShowAnswerScore=enableShowAnswerScore;
    }


    public void initData(int position,final AnswerSheetBean answerSheetBean){
        TextView tv_kindName=itemView.findViewById(R.id.tv_kind_name);
        tv_kindName.setText(answerSheetBean.getPartMsg());
        TextView tv_section=itemView.findViewById(R.id.tv_section);
        tv_section.setText(answerSheetBean.getSectionMsg());
        tv_kindName.setVisibility(TextUtils.isEmpty(answerSheetBean.getPartMsg())?View.GONE:View.VISIBLE);
        tv_section.setVisibility(TextUtils.isEmpty(answerSheetBean.getSectionMsg())?View.GONE:View.VISIBLE);
        View view_head = itemView.findViewById(R.id.view_head);
        view_head.setVisibility(TextUtils.isEmpty(answerSheetBean.getPartMsg())&&
                TextUtils.isEmpty(answerSheetBean.getSectionMsg())?View.GONE:View.VISIBLE);

        ChildFlowLayout childFlowLayout = itemView.findViewById(R.id.child_flow_layout);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childFlowLayout.getLayoutParams();
        if(TextUtils.isEmpty(answerSheetBean.getPartMsg())&&
                TextUtils.isEmpty(answerSheetBean.getSectionMsg())){
            layoutParams.topMargin=-DpUtils.dip2px(childFlowLayout.getContext(),5);
        }else{
            layoutParams.topMargin=0;
        }
        childFlowLayout.setLayoutParams(layoutParams);

        childFlowLayout.removeAllViews();
        for (AnswerSheetChildBean answerSheetChildBean:answerSheetBean.getAnswerSheetChildBeanList()){
            childFlowLayout.addView(addView(answerSheetChildBean));
        }
        childFlowLayout.setOnItemClickListener((position1, view) -> {
            if(System.currentTimeMillis()- ConstantBean.Companion.getOnClickSubTime()<200){
                return;
            }
            ConstantBean.Companion.setOnClickSubTime(System.currentTimeMillis());
            AnswerSheetChildBean bean = answerSheetBean.getAnswerSheetChildBeanList().get(position1);
            if(bean.getShowState()==5||bean.getShowState()==6){
                return;
            }
            onClickAnswerSheet(bean);
        });
    }

    public View addView(AnswerSheetChildBean bean){
        LayoutInflater layoutInflater = LayoutInflater.from(itemView.getContext());
        @SuppressLint("InflateParams") View inflate = layoutInflater .inflate(R.layout.ev_item_child_answer,null);
        TextView textView=inflate.findViewById(R.id.tv_child_index);
        textView.setText(bean.getSortIndex());
        ImageView imageView=inflate.findViewById(R.id.img_select_flag);
        imageView.setVisibility(bean.getIsSelected()==0?View.INVISIBLE:View.VISIBLE);
        textView.setTextColor(0xffffffff);
        inflate.findViewById(R.id.img_mark).setVisibility(bean.isMark()?View.VISIBLE:View.GONE);
        switch (bean.getShowState()){
            case 1:
            case 4:
                if( ConstantBean.Companion.getTaskState()== TaskState.LOOKING){
                    textView.setBackgroundResource(R.mipmap.ev_icon_choice_blue);
                }else{
                    textView.setBackgroundResource(R.mipmap.ev_icon_test_choice_blue);
                }
                imageView.setImageResource(R.drawable.ev_vector_up_arror_09f);
                break;
            case 2:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_red);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_red);
                break;
            case 3:
                textView.setBackgroundResource(R.mipmap.ev_icon_choice_green);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_green);
                break;
            case 5:
                textView.setTextColor(0xffc3c3c3);
                textView.setBackgroundResource(R.mipmap.ev_icon_test_unenable_answer);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
                break;
            case 6:
                textView.setBackgroundResource(R.mipmap.ev_icon_test_unenable_answer_blue);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
                break;
            default:
                textView.setTextColor(0xff666666);
                textView.setBackgroundResource(R.mipmap.ev_icon_test_choice_default);
                imageView.setImageResource(R.drawable.ev_vector_up_arror_ccc);
        }
        return inflate;
    }
}
